<?php

class Item {
    private $id;
    private $label;
    private $url;
    private $description;

    public function __construct($id, $label, $url = null, $description = null) {
        $this->id = $id;
        $this->label = $label;
        $this->url = $url;
        $this->description = $description;
    }

    public function getId() {
        return $this->id;
    }

    public function getLabel() {
        return $this->label;
    }

    public function getUrl() {
        return $this->url;
    }

    public function getDescription() {
        return $this->description;
    }

}

class Menu {

    private $id = null;
    private $class = null;
    private $items = null;

    public function __construct($id, $class = null) {
        $this->id = $id;
        $this->class = $class;
        $this->items = array();
    }

    public function addItem($id, $label, $url = null, $description = null) {
        $this->items[] = new Item($id, $label, $url, $description);
    }

    public function flush($xmlDoc, $selected = null) {
        if(is_array($this->items)) {
            $menuAttrs = array();
            if($this->class) {
                $menuAttrs['class'] = 'menu ' . $this->class;
            }
            else {
                $menuAttrs['class'] = 'menu';
            }
            $xmlDoc->elementStart('div', $this->id);
            $xmlDoc->elementStart('ul', $menuAttrs);
            foreach($this->items as $item) {
                $itemAttrs = array('id' => $item->getId());
                if($item->getId() == $selected) {
                    $itemAttrs['class'] = 'current';
                }
                $xmlDoc->elementStart('li', $itemAttrs);
                $linkAttrs = array('href' => $item->getUrl());
                if($description = $item->getDescription()) {
                    $linkAttrs['title'] = $description;
                }
                $xmlDoc->element('a', $linkAttrs, $item->getLabel());
                $xmlDoc->elementEnd('li');
            }
            $xmlDoc->elementEnd('ul');
            $xmlDoc->elementEnd('div');
        }
    }

}
